﻿using System;
using System.Collections.Generic;
using System.Linq;
using SAPbobsCOM;
using System.Text;

namespace CompanySyncProject
{
    class DIUdo : BaseRun
    {
        public DIUdo(Company co, System.IO.StreamWriter lo)
            : base(co, lo)
        {

        }

        public override void Run()
        {
            oCompany.XmlExportType = BoXmlExportTypes.xet_ExportImportMode;
           
            
            SAPbobsCOM.GeneralService oGeneralService = oCompany.GetCompanyService().GetGeneralService("DOC01");
            /*
            //add UDO by filling in the UDO
            SAPbobsCOM.GeneralData oGeneralData = oGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData);
            oGeneralData.SetProperty("Remark", "some remark");
            oGeneralData.SetProperty("U_FIELD01", "field 01");
            oGeneralData.SetProperty("U_FIELD02", "field 02");

            SAPbobsCOM.GeneralDataCollection oChildren01 = oGeneralData.Child("DOCC01");
            SAPbobsCOM.GeneralData oChild0101 = oChildren01.Add();
            oChild0101.SetProperty("U_FIELD01", "field 01");
            oChild0101.SetProperty("U_FIELD02", "field 02");
            oChild0101 = oChildren01.Add();
            oChild0101.SetProperty("U_FIELD01", "field 03");
            oChild0101.SetProperty("U_FIELD02", "field 04");

            SAPbobsCOM.GeneralDataCollection oChildren02 = oGeneralData.Child("DOCC02");
            SAPbobsCOM.GeneralData oChild0102 = oChildren02.Add();
            oChild0102.SetProperty("U_FIELD01", "field 01");
            oChild0102.SetProperty("U_FIELD02", "field 02");
            oChild0102 = oChildren02.Add();
            oChild0102.SetProperty("U_FIELD01", "field 03");
            oChild0102.SetProperty("U_FIELD02", "field 04");

            string xml = oGeneralData.ToXMLString();

            //oGeneralService.Add(oGeneralData);
            */

            /*
            //add UDO by XML
            SAPbobsCOM.GeneralData oGeneralData = oGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData);
            oGeneralData.FromXMLString(@"<DOC01><U_FIELD01>abc</U_FIELD01><DOCC01Collection><DOCC01><U_FIELD01>aaa</U_FIELD01><U_FIELD02>bbb</U_FIELD02></DOCC01></DOCC01Collection><DOCC02Collection><DOCC02><U_FIELD01>aaa</U_FIELD01><U_FIELD02>bbb</U_FIELD02></DOCC02></DOCC02Collection></DOC01>");
            oGeneralService.Add(oGeneralData);
            */

            /*
            //update UDO by filling in the UDO
            SAPbobsCOM.GeneralDataParams oGeneralParams = oGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralDataParams);
            oGeneralParams.SetProperty("DocEntry", 3);
            SAPbobsCOM.GeneralData oGeneralData = oGeneralService.GetByParams(oGeneralParams);
            oGeneralData.SetProperty("Remark", "bcd");
            oGeneralService.Update(oGeneralData);
            */

            //update UDO by filling in the UDO Child
            SAPbobsCOM.GeneralDataParams oGeneralParams = oGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralDataParams);
            oGeneralParams.SetProperty("DocEntry", 2);
            SAPbobsCOM.GeneralData oGeneralData = oGeneralService.GetByParams(oGeneralParams);

            string output = oGeneralData.ToXMLString();

            //oGeneralData.FromXMLString(@"<DOC01><U_FIELD01>abc</U_FIELD01><DOCC01Collection><DOCC01><LineId>3</LineId></DOCC01><DOCC01><LineId>4</LineId><U_FIELD01>aaa</U_FIELD01><U_FIELD02>bbb</U_FIELD02></DOCC01></DOCC01Collection></DOC01>");
            SAPbobsCOM.GeneralDataCollection oChildren01 = oGeneralData.Child("DOCC01");
            SAPbobsCOM.GeneralData oChild01 = oChildren01.Item(0);

            string schema = oGeneralData.GetXMLSchema();

            oChild01.SetProperty("U_FIELD01", "aaa");
            
            oGeneralService.Update(oGeneralData);

            int i = 0;
        }
    }
}
